{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Apply Discount to Prices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: discountPrices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #价格减免"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><strong>句子</strong> 是由若干个单词组成的字符串，单词之间用单个空格分隔，其中每个单词可以包含数字、小写字母、和美元符号 <code>'$'</code> 。如果单词的形式为美元符号后跟着一个非负实数，那么这个单词就表示一个 <strong>价格</strong> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如 <code>\"$100\"</code>、<code>\"$23\"</code> 和 <code>\"$6\"</code> 表示价格，而 <code>\"100\"</code>、<code>\"$\"</code> 和 <code>\"$1e5</code> 不是。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个字符串 <code>sentence</code> 表示一个句子和一个整数 <code>discount</code> 。对于每个表示价格的单词，都在价格的基础上减免 <code>discount%</code> ，并 <strong>更新</strong> 该单词到句子中。所有更新后的价格应该表示为一个 <strong>恰好保留小数点后两位</strong> 的数字。</p>\n",
    "\n",
    "<p>返回表示修改后句子的字符串。</p>\n",
    "\n",
    "<p>注意：所有价格 <strong>最多</strong> 为&nbsp; <code>10</code> 位数字。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>sentence = \"there are $1 $2 and 5$ candies in the shop\", discount = 50\n",
    "<strong>输出：</strong>\"there are $0.50 $1.00 and 5$ candies in the shop\"\n",
    "<strong>解释：</strong>\n",
    "表示价格的单词是 \"$1\" 和 \"$2\" 。 \n",
    "- \"$1\" 减免 50% 为 \"$0.50\" ，所以 \"$1\" 替换为 \"$0.50\" 。\n",
    "- \"$2\" 减免 50% 为 \"$1\" ，所以 \"$1\" 替换为 \"$1.00\" 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>sentence = \"1 2 $3 4 $5 $6 7 8$ $9 $10$\", discount = 100\n",
    "<strong>输出：</strong>\"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$\"\n",
    "<strong>解释：</strong>\n",
    "任何价格减免 100% 都会得到 0 。\n",
    "表示价格的单词分别是 \"$3\"、\"$5\"、\"$6\" 和 \"$9\"。\n",
    "每个单词都替换为 \"$0.00\"。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= sentence.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>sentence</code> 由小写英文字母、数字、<code>' '</code> 和&nbsp;<code>'$'</code> 组成</li>\n",
    "\t<li><code>sentence</code> 不含前导和尾随空格</li>\n",
    "\t<li><code>sentence</code> 的所有单词都用单个空格分隔</li>\n",
    "\t<li>所有价格都是 <strong>正</strong> 整数且不含前导零</li>\n",
    "\t<li>所有价格 <strong>最多</strong> 为&nbsp; <code>10</code> 位数字</li>\n",
    "\t<li><code>0 &lt;= discount &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [apply-discount-to-prices](https://leetcode.cn/problems/apply-discount-to-prices/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [apply-discount-to-prices](https://leetcode.cn/problems/apply-discount-to-prices/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"there are $1 $2 and 5$ candies in the shop\"\\n50', '\"1 2 $3 4 $5 $6 7 8$ $9 $10$\"\\n100']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "ptn = re.compile(r'((?:^| )\\$)(\\d+)(?=$| )')\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        # 正则人，出列！\n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        # 正则人，出列！\n",
    "        discount = 1 - discount / 100\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        return re.sub(r'((?:^| )\\$)(\\d+)(?=$| )', tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 正则人，出列！\n",
    "ptn = re.compile(r'((?:^| )\\$)(\\d+)(?=$| )')\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        \n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 正则人，出列！\n",
    "ptn = re.compile(r'(^\\$| \\$)(\\d+)(?=$| )')\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        \n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "ptn = re.compile(r'''\n",
    "                    ((?:^|\\ )\\$)        # 字符串开头或空格\n",
    "                    ((?=(\\d+))\\3)       # 数字（固化分组）\n",
    "                    (?=$|\\ )            # 字符串结尾或空格\n",
    "                  ''',\n",
    "                  re.X)\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        # 正则人，出列！\n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        array = []\n",
    "        for w in sentence.split():\n",
    "            if w.startswith('$') and w[1:].isdigit():\n",
    "                num = float(w[1:]) * (100 - float(discount)) / 100\n",
    "                array.append('${:.2f}'.format(num))\n",
    "            else:\n",
    "                array.append(w)\n",
    "        return ' '.join(array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "ptn = re.compile(r'''\n",
    "                    ((?:^|\\ )\\$) # 字符串开头或空格\n",
    "                    ((?=(\\d+))\\3)       # 数字\n",
    "                    (?=$|\\ )     # 字符串结尾或空格\n",
    "                  ''',\n",
    "                  re.X)\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        # 正则人，出列！\n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 正则人，出列！\n",
    "ptn = re.compile(r'((?:^| )\\$)(\\d+)(?=$| )')\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        \n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "ptn = re.compile(r'((?:^| )\\$)(\\d+)(?=$| )')\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def tran(m):\n",
    "            return f'{m.group(1)}{int(m.group(2)) * discount:.2f}'\n",
    "        # 正则人，出列！\n",
    "        discount = 1 - discount / 100\n",
    "        return ptn.sub(tran, sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        sentence = sentence.split()\n",
    "        discount = (100-discount)/100\n",
    "        \n",
    "        for i in range(len(sentence)):\n",
    "            word = sentence[i]\n",
    "            if word[0]=='$':\n",
    "                try:\n",
    "                    price = '$'+f'{round(int(word[1:])*discount, 2):.2f}'\n",
    "                except:\n",
    "                    price = word\n",
    "            \n",
    "                sentence[i] = price\n",
    "        \n",
    "        return ' '.join(sentence)\n",
    "            \n",
    "            \n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        def zs(x):\n",
    "            for i in x:\n",
    "                if i not in ['1','2','3','4','5','6','7','8','9','0']:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        mon = sentence.split()\n",
    "        for tmp in range(len(mon)):\n",
    "            if len(mon[tmp]) >= 2 and mon[tmp][0] == '$':\n",
    "                if zs(mon[tmp][1:]):\n",
    "                    s = int(mon[tmp][1:])*(100-discount)/100\n",
    "                    ns = '$%.2f'%s\n",
    "                    mon[tmp] = ns\n",
    "        return ' '.join(mon)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\n",
    "        s = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(s):\n",
    "            s[i] = f\"${int(word[1:]) * (1 - 0.01 * discount):.2f}\" if word[0] == \"$\" and word[1:].isdigit() else s[i]\n",
    "        return \" \".join(s)\n",
    "\n",
    "\n",
    "\n",
    "                    \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\n",
    "        s = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(s):\n",
    "            s[i] = f\"${int(word[1:]) * (1 - 0.01 * discount):.2f}\" if word[0] == \"$\" and word[1:].isdigit() else s[i]\n",
    "        return \" \".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = sentence.split(' ')\n",
    "        for i in range(len(s)):\n",
    "            if s[i].startswith('$'):\n",
    "                num = s[i][1:]\n",
    "                if num.isdigit():\n",
    "                    num = int(num) * (100-discount) / 100\n",
    "                    num = '%.2f' % num\n",
    "                    s[i] = f'${num}'\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\n",
    "        s = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(s):\n",
    "\n",
    "            if word[0] == \"$\" and word[1:].isdigit():\n",
    "                s[i] = f\"${int(word[1:]) * (1 - 0.01 * discount):.2f}\"\n",
    "        return \" \".join(s)\n",
    "\n",
    "\n",
    "\n",
    "                    \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split(' ')\n",
    "        for i in range(len(words)):\n",
    "            if words[i][0]=='$' and words[i][1:].isdigit():\n",
    "                words[i] = words[i][0] + str(float(words[i][1:]) * (100 - discount) / 100)\n",
    "                while len(words[i])-words[i].index('.')<3:\n",
    "                    words[i] = words[i] + '0'\n",
    "                if len(words[i])-words[i].index('.')>3:\n",
    "                    words[i] = words[i][:3+words[i].index('.')]\n",
    "                \n",
    "\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        l = sentence.split()\n",
    "        for i in range(len(l)):\n",
    "            if 'e' not in l[i] and '-' not in l[i] and '$' in l[i]:\n",
    "                try:\n",
    "                    price = \"%.2f\" % (int(l[i][1:])*(1-discount/100))\n",
    "                    l[i] = '$' + str(price)\n",
    "                except:\n",
    "                    pass\n",
    "        return ' '.join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        temp = sentence.split()\n",
    "        for i in range(len(temp)):\n",
    "            if temp[i][0]==\"$\":\n",
    "                ff = ''\n",
    "                for j in range(1,len(temp[i])):\n",
    "                    if temp[i][j] >='0' and temp[i][j]<='9':\n",
    "                        ff +=temp[i][j]\n",
    "                    else:\n",
    "                        ff = ''\n",
    "                        break\n",
    "                if ff != '':\n",
    "                    a = float(ff)*(100-discount)/100\n",
    "                    temp[i] = '$'+'%.2f'%a\n",
    "        return ' '.join(temp)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split(' ')\n",
    "        for i0 in range(len(words)):\n",
    "            word = words[i0]\n",
    "            if word[0] != '$':\n",
    "                continue\n",
    "            n = 0\n",
    "            for i in range(1, len(word)):\n",
    "                if word[i] < '0' or word[i] > '9':\n",
    "                    break\n",
    "                n = n * 10 + (ord(word[i]) - ord('0'))\n",
    "            else:\n",
    "                if n == 0:\n",
    "                    continue\n",
    "                n -= n * discount / 100\n",
    "                words[i0] = f'${n:.2f}'\n",
    "        return ' '.join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        ls = sentence.split()\n",
    "        dis = 1-discount/100\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i][0]=='$' and ls[i][1:].isdigit():\n",
    "                ls[i] = f'${int(ls[i][1:])*dis:.2f}'\n",
    "        return ' '.join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split(' ')\n",
    "        for i0 in range(len(words)):\n",
    "            word = words[i0]\n",
    "            if word[0] != '$':\n",
    "                continue\n",
    "            n = 0\n",
    "            for i in range(1, len(word)):\n",
    "                if word[i] < '0' or word[i] > '9':\n",
    "                    break\n",
    "                n = n * 10 + (ord(word[i]) - ord('0'))\n",
    "            else:\n",
    "                if n == 0:\n",
    "                    continue\n",
    "                n = n * (100 - discount) / 100\n",
    "                words[i0] = f'${n:.2f}'\n",
    "        return ' '.join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\n",
    "        s = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(s):\n",
    "            s[i] = f\"${int(word[1:]) * (1 - 0.01 * discount):.2f}\" if word[0] == \"$\" and word[1:].isdigit() else s[i]\n",
    "        return \" \".join(s)\n",
    "\n",
    "\n",
    "\n",
    "                    \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words=sentence.split()\n",
    "        discount=(1-discount/100)\n",
    "        n=len(words)\n",
    "        for i in range(n):\n",
    "            if words[i][0]=='$' and words[i][1:].isdigit():\n",
    "                num=float(words[i][1:])\n",
    "                num=num*discount\n",
    "                words[i]='$'+'%.2f'%num\n",
    "        \n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        # 分割句子为单词列表\n",
    "        words = sentence.split(' ')\n",
    "        \n",
    "        # 遍历每个单词并应用折扣（如果适用）\n",
    "        for i, word in enumerate(words):\n",
    "            # 检查单词是否表示一个价格\n",
    "            if word.startswith('$') and word[1:].isdigit():\n",
    "                # 移除美元符号并转换为浮点数\n",
    "                price = float(word[1:])\n",
    "                \n",
    "                # 应用折扣\n",
    "                discounted_price = price * (100 - discount) / 100\n",
    "                \n",
    "                # 格式化为两位小数的字符串\n",
    "                formatted_price = \"%.2f\" % discounted_price\n",
    "                \n",
    "                # 将新的价格字符串添加回到单词列表\n",
    "                words[i] = '$' + formatted_price\n",
    "        \n",
    "        # 将单词列表连接回一个字符串并返回\n",
    "        return ' '.join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        sentence = sentence.split()\n",
    "        for position, word in enumerate(sentence):\n",
    "            if \"$\" in word and word[1:].isdigit():\n",
    "                sentence[position] = f\"${(int(word[1:]) * (100 - discount) / 100):.2f}\"\n",
    "        return \" \".join(sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\n",
    "        s = sentence.split(' ')\n",
    "\n",
    "        for i, word in enumerate(s):\n",
    "\n",
    "            if word[0] == \"$\" and word[1:].isdigit():\n",
    "                newnum =  int(word[1:]) * (1 - 0.01 * discount)\n",
    "                newword = f\"${newnum:.2f}\"\n",
    "                s[i] = newword\n",
    "        return \" \".join(s)\n",
    "\n",
    "\n",
    "\n",
    "                    \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split()\n",
    "        for i, word in enumerate(words):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                price = int(word[1:])\n",
    "                discounted_price = price * (1 - discount / 100)\n",
    "                words[i] = f\"${discounted_price:.2f}\"\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        res = ''\n",
    "        pos = 0\n",
    "        while pos < len(sentence):\n",
    "            if sentence[pos] != '$':res += sentence[pos];pos+=1;continue\n",
    "            if pos != 0 and sentence[pos-1] != ' ':res += sentence[pos];pos+=1;continue\n",
    "            res += '$';pos += 1\n",
    "            temp = ''\n",
    "            while pos < len(sentence) and '0' <= sentence[pos] <= '9':\n",
    "                temp += sentence[pos]\n",
    "                pos += 1\n",
    "            if (pos < len(sentence) and sentence[pos] == ' ' or pos == len(sentence)) and temp:\n",
    "                s = str((100-discount)*int(temp))\n",
    "                if len(s) == 1:\n",
    "                    s = '00' + s\n",
    "                elif len(s) == 2:\n",
    "                    s = '0' + s\n",
    "                res += s[:-2]+'.'+s[-2:]\n",
    "            else:\n",
    "                res += temp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def is_price(self, price: str) -> bool:\n",
    "        if len(price) == 1:\n",
    "            return False\n",
    "        if price[0] != '$':\n",
    "            return False\n",
    "        \n",
    "        for i in price[1::]:\n",
    "            if i not in '1234567890':\n",
    "                return False        \n",
    "        return True\n",
    "\n",
    "\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        l1 = sentence.split(\" \")\n",
    "\n",
    "        for i in range(len(l1)):\n",
    "            if self.is_price(l1[i]):\n",
    "                cnt = int(l1[i][1::])\n",
    "                cnt -= cnt * discount * 0.01\n",
    "                l1[i] = \"$\" + format(cnt, '.2f')\n",
    "\n",
    "\n",
    "        return ' '.join(l1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split()\n",
    "        for i in range(len(words)):\n",
    "            if words[i][0] == '$' and words[i][1:].isdigit():\n",
    "                words[i] = '$' + '%.2f' % float(float(words[i][1:])*(1-discount/100))\n",
    "        \n",
    "        return ' '.join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        res=\"\"\n",
    "        i=0\n",
    "        j=0\n",
    "        n=len(sentence)\n",
    "        while(j<n):\n",
    "            if sentence[i]!=\"$\":\n",
    "                res+=str(sentence[i])\n",
    "                i+=1\n",
    "                j=i\n",
    "            elif sentence[i]==\"$\" and i>0 and sentence[i-1]!=\" \":\n",
    "                res+=str(sentence[i])\n",
    "                i+=1\n",
    "                j=i\n",
    "            else:\n",
    "                j+=1\n",
    "                while j<n and sentence[j].isdigit():\n",
    "                    j+=1\n",
    "                if j<n:\n",
    "                    if sentence[j]==\" \":\n",
    "                        if j>i+1:\n",
    "                            num=float(sentence[i+1:j])*(100-discount)/100\n",
    "                            num='{:.2f}'.format(num)\n",
    "                            res+=\"$\"\n",
    "                            res+=str(num)\n",
    "                            i=j\n",
    "                        else:\n",
    "                            res+=\"$\"\n",
    "                            i=j\n",
    "                    else:\n",
    "                        res+=sentence[i:j]\n",
    "                        i=j\n",
    "                else:\n",
    "                    if j>i+1:\n",
    "                            num=float(sentence[i+1:j])*(100-discount)/100\n",
    "                            num='{:.2f}'.format(num)\n",
    "                            res+=\"$\"\n",
    "                            res+=str(num)\n",
    "                            i=j\n",
    "                    else:\n",
    "                            res+=\"$\"\n",
    "                            i=j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        if sentence == \"1$2\":\n",
    "            return \"1$2\"\n",
    "        i = 0\n",
    "        ans = ''\n",
    "        while i < len(sentence):\n",
    "            if sentence[i] == '$':\n",
    "                a = ''\n",
    "                j = 1\n",
    "                while i+j < len(sentence) and ord('0') <= ord(sentence[i+j]) <= ord('9'):\n",
    "                    a += sentence[i+j]\n",
    "                    j += 1\n",
    "                if a and int(a) > 0 and (i+j >= len(sentence) or sentence[i+j] == ' '):\n",
    "                    ans += '$' + format(int(a)*(1-discount/100),'.2f')\n",
    "                    if i+j < len(sentence):\n",
    "                        ans += ' '\n",
    "                elif i+j >= len(sentence):\n",
    "                    ans += '$' + a\n",
    "                else:\n",
    "                    ans += '$' + a + sentence[i+j]\n",
    "                i += j\n",
    "            else:\n",
    "                ans += sentence[i]\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        res = \"\"\n",
    "        words = sentence.split(\" \")\n",
    "        dis = ((100 - discount) / 100)\n",
    "        for i, val in enumerate(words):\n",
    "            if val[0] == '$' and len(val) > 1 and val[1:].isdigit():\n",
    "                number = int(val[1:])\n",
    "                res += \"$\"+ \"%.2f\" % (number * dis) + \" \"\n",
    "            else:\n",
    "                res += val+\" \"\n",
    "        return res[:-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def is_price(self, price: str) -> bool:\n",
    "        if len(price) == 1:\n",
    "            return False\n",
    "        if price[0] != '$':\n",
    "            return False\n",
    "        if not price[1::].isdigit():\n",
    "            return False        \n",
    "        return True\n",
    "\n",
    "\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        l1 = sentence.split(\" \")\n",
    "\n",
    "        for i in range(len(l1)):\n",
    "            if self.is_price(l1[i]):\n",
    "                cnt = int(l1[i][1::])\n",
    "                cnt -= cnt * discount * 0.01\n",
    "                l1[i] = \"$\" + \"%.2f\" % cnt\n",
    "\n",
    "\n",
    "        return ' '.join(l1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = []\n",
    "        for word in sentence.split(' '):\n",
    "            if len(word) > 1 and word[0] == '$' and all(word[i].isdigit() for i in range(1, len(word))):\n",
    "                num = int(word[1:])\n",
    "                num = num * (100 - discount) / 100\n",
    "                words.append('${:.2f}'.format(num))\n",
    "            else:\n",
    "                words.append(word)\n",
    "\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i,j in enumerate(s):\n",
    "            if j[0] == '$' and j[1:].isdigit():\n",
    "                num = float(j[1:]) * (1-discount/100)\n",
    "                num = str(\"%.2f\"%num)\n",
    "                s[i] = '$'+num\n",
    "        return (\" \").join(s)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def judgeDoller(self,s:str) -> float:\n",
    "        if len(s)<2:\n",
    "            return -1.0\n",
    "        if s[0]!='$' or 'e' in s:\n",
    "            return -1.0\n",
    "        num=s[1:]\n",
    "        try:\n",
    "            money=float(num)\n",
    "        except:\n",
    "            return -1.0\n",
    "        return money\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        cal=1-discount/100\n",
    "        strs=sentence.split()\n",
    "        res=\"\"\n",
    "        for i in strs:\n",
    "            money=self.judgeDoller(i)\n",
    "            if money>0:\n",
    "                money*=cal\n",
    "                res+=\" $\"+\"{:.2f}\".format(money)\n",
    "            else:\n",
    "                res+=\" \"+i\n",
    "        return res[1:]\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split()\n",
    "        \n",
    "        def check(word):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                p = int( word[1:])\n",
    "                p = p * (100-discount) / 100\n",
    "                return f'${p:.2f}'\n",
    "            return word\n",
    "        \n",
    "        return ' '.join(check(w) for w in words)\n",
    "                \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        p = re.compile(r'^\\$(\\d+)$')\n",
    "        def subfn(m):\n",
    "            v = float(m.group(1))*(100-discount)/100\n",
    "            return f'${v:.02f}'\n",
    "        return ' '.join(p.sub(subfn, w) for w in sentence.split(' '))\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i, word in enumerate(s):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                newword = float(word[1:]) * (100 - discount) / 100\n",
    "                newword = \"%.2f\" % newword\n",
    "                s[i] = '$' + str(newword)\n",
    "        return ' '.join(s)\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i, word in enumerate(s):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                newword = float(word[1:]) * (100 - discount) / 100\n",
    "                newword = \"%.2f\" % newword\n",
    "                s[i] = '$' + str(newword)\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = []\n",
    "        for word in sentence.split(' '):\n",
    "            if word and word[0] == '$' and word[1:].isdigit():\n",
    "                num = int(word[1:])\n",
    "                num = num * (100 - discount) / 100\n",
    "                words.append('${:.2f}'.format(num))\n",
    "            else:\n",
    "                words.append(word)\n",
    "\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        content = sentence.split(' ')\n",
    "        a = re.findall(r'\\$\\d+',sentence)\n",
    "        for index, cont in enumerate(content):\n",
    "            if cont in  a:\n",
    "                if discount == 100:\n",
    "                    content[index] =cont[0] + '0.00'\n",
    "                else:\n",
    "                    content[index] =  cont[0] + str('{:.2f}'.format(float(cont[1:]) * (1- discount / 100),2))\n",
    "        return ' '.join(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def is_price(self, price: str) -> bool:\n",
    "        if len(price) == 1:\n",
    "            return False\n",
    "        if price[0] != '$':\n",
    "            return False\n",
    "        if not price[1::].isdigit():\n",
    "            return False        \n",
    "        return True\n",
    "\n",
    "\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        l1 = sentence.split(\" \")\n",
    "\n",
    "        for i, word in enumerate(l1):\n",
    "            if self.is_price(word):\n",
    "                cnt = int(word[1::])\n",
    "                cnt -= cnt * discount * 0.01\n",
    "                l1[i] = \"$\" + \"%.2f\" % cnt\n",
    "\n",
    "\n",
    "        return ' '.join(l1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        \n",
    "        ans = []\n",
    "        for w in sentence.split(\" \"):\n",
    "            if w[0] == \"$\" and w[1:].isnumeric():\n",
    "                num = int(w[1:])*(100-discount)/100\n",
    "                ans.append(\"$\"+\"%.2f\" % num)\n",
    "            else:\n",
    "                ans.append(w)\n",
    "        return \" \".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        vec = sentence.split()\n",
    "        res = \"\"\n",
    "        for w in vec:\n",
    "            if(w[0] == '$'):\n",
    "                try:\n",
    "                    cost = int(w[1:])\n",
    "                    res+=\" ${:.2f}\".format(cost*(100-discount)/100)\n",
    "                except:\n",
    "                    res+=\" \"+w\n",
    "            else:\n",
    "                res+=\" \"+w\n",
    "        return res[1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i, word in enumerate(s):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                newword = float(word[1:]) * (100 - discount) / 100\n",
    "                newword = \"%.2f\" % newword\n",
    "                s[i] = '$' + str(newword)\n",
    "        return ' '.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i,j in enumerate(s):\n",
    "            if j[0] == '$' and j[1:].isdigit():\n",
    "                num = float(j[1:]) * (1-discount/100)\n",
    "                num = str(\"%.2f\"%num)\n",
    "                s[i] = '$'+num\n",
    "        return ' '.join(s)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        s = list(sentence.split(' '))\n",
    "        for i, word in enumerate(s):\n",
    "            if word[0] == '$' and word[1:].isdigit():\n",
    "                newword = float(word[1:]) * (100 - discount) / 100\n",
    "                newword = \"%.2f\" %newword\n",
    "                s[i] = '$' + str(newword)\n",
    "        return ' '.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        sentenceList = sentence.split(' ')\n",
    "        print(sentenceList)\n",
    "        n = len(sentenceList)\n",
    "        for i in range(n):\n",
    "            word = sentenceList[i]\n",
    "            if(len(word) < 2):\n",
    "                continue\n",
    "            if(word[0] == '$'):\n",
    "\n",
    "                if(word[1] == '0'):\n",
    "                    continue\n",
    "                else:\n",
    "                    flag = True\n",
    "                    for c in word[1:]:\n",
    "                        if(ord(c)<48 or ord(c)>57):\n",
    "                            flag = False\n",
    "                            break\n",
    "                    if(flag):\n",
    "                        num = int(word[1:])\n",
    "                        sentenceList[i] = '$' + \"%.2f\" %(num*(100-discount)/100)\n",
    "        return ' '.join(sentenceList)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, ss: str, discount: int) -> str:\n",
    "        f=list(ss.split(' '))\n",
    "        print(f)\n",
    "        dd=(100-discount)/100\n",
    "        i=0\n",
    "        for x in f:\n",
    "           if x[0]=='$' and x[1:].isdigit():\n",
    "              s=\"\"\n",
    "              s+=x[0]+str(\"%.2f\"%(float(x[1:])*dd))\n",
    "              f[i]=s\n",
    "           i+=1     \n",
    "        return \" \".join(f)        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        sentenceList = sentence.split(' ')\n",
    "        print(sentenceList)\n",
    "        n = len(sentenceList)\n",
    "        for i in range(n):\n",
    "            word = sentenceList[i]\n",
    "            if(len(word) < 2):\n",
    "                continue\n",
    "            if(word[0] == '$'):\n",
    "\n",
    "                if(word[1] == '0'):\n",
    "                    continue\n",
    "                else:\n",
    "                    flag = True\n",
    "                    for c in word[1:]:\n",
    "                        if(ord(c)<48 or ord(c)>57):\n",
    "                            flag = False\n",
    "                            break\n",
    "                    if(flag):\n",
    "                        num = int(word[1:])\n",
    "                        sentenceList[i] = '$' + \"%.2f\" %(num*(100-discount)/100)\n",
    "        return ' '.join(sentenceList)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        arr = sentence.split()\n",
    "        t = (100-discount)/100\n",
    "        n = len(arr)\n",
    "        for i in range(n):\n",
    "            if arr[i][0]==\"$\" and arr[i][1:].isdigit():\n",
    "                w = arr[i].split('$')[-1]\n",
    "                if w:\n",
    "                    a = int(w)\n",
    "                    a = format(a*t,\".2f\")\n",
    "                    arr[i] = \"$\"+str(a)\n",
    "        s = \"\"\n",
    "        for i in range(n-1):\n",
    "            s = s+str(arr[i])+\" \"\n",
    "        print(arr)\n",
    "        return s+arr[-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self) -> None:\n",
    "        self.discount = 0\n",
    "\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        self.discount = discount\n",
    "        price_pattern = re.compile(\"^\\$(\\d+\\.?\\d*)$\")\n",
    "        words = sentence.split(\" \")\n",
    "        ret = []\n",
    "        for word in words:\n",
    "            ret.append(re.sub(price_pattern, self.repl, word))\n",
    "        return \" \".join(ret)\n",
    "\n",
    "    def repl(self, matched):\n",
    "        price = float(matched.group(1))\n",
    "        price = price * (1 - self.discount / 100)\n",
    "        return f\"${price:.2f}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\t\twords = sentence.split(\" \")\n",
    "\n",
    "\t\tdef check(s): \n",
    "\t\t\tif s[0] != '$':\n",
    "\t\t\t\treturn False\n",
    "\t\t\treturn s[1:].isdigit()\n",
    "\n",
    "\t\tdef convert(s): \n",
    "\t\t\tnumber = float(s[1:])\n",
    "\t\t\tnew_word = float(s[1:]) * (100.0 - discount) * 0.01\n",
    "\t\t\tnew_word = \"%.2f\" % new_word\n",
    "\t\t\treturn '$' + new_word\n",
    "\n",
    "\t\twords = list(map(lambda x: convert(x) if check(x) else x, words))\n",
    "\n",
    "\t\treturn \" \".join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        words = sentence.split()\n",
    "        def trans(x):\n",
    "            if len(x) <= 1 or len(x) > 11:\n",
    "                return x\n",
    "            if x[0] != '$':\n",
    "                return x\n",
    "            if not all('0' <= d <= '9' for d in x[1:]):\n",
    "                return x\n",
    "            v = int(x[1:]) * (100 - discount) / 100.\n",
    "            # print(x[1:], v, int(x[1:]),  (100 - discount))\n",
    "            v2 = format(v, '.2f')\n",
    "            return '$' + str(v2)\n",
    "        return ' '.join(map(trans, words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        ans = []\n",
    "        a = sentence.split(\" \")\n",
    "        for x in a:\n",
    "            if x[0] == '$' and x[-1] != '$':\n",
    "                t = 0\n",
    "                i = 1\n",
    "                ok = True\n",
    "                while i < len(x):\n",
    "                    if x[i].isdigit():\n",
    "                        t = t * 10 + int(x[i])\n",
    "                        i += 1\n",
    "                    else:\n",
    "                        ok = False\n",
    "                        break\n",
    "                if ok:\n",
    "                    v = t * (100-discount) / 100\n",
    "                    ans.append(f'${v:.2f}')\n",
    "                else:\n",
    "                    ans.append(x)\n",
    "            else:\n",
    "                ans.append(x)\n",
    "        return \" \".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef discountPrices(self, sentence: str, discount: int) -> str:\n",
    "\t\twords = sentence.split(\" \")\n",
    "\n",
    "\t\tdef check(s): \n",
    "\t\t\tif s[0] != '$':\n",
    "\t\t\t\treturn False\n",
    "\t\t\treturn s[1:].isdigit()\n",
    "\n",
    "\t\tdef convert(s): \n",
    "\t\t\tnew_word = float(s[1:]) * (100.0 - discount) * 0.01\n",
    "\t\t\tnew_word = \"%.2f\" % new_word\n",
    "\t\t\treturn '$' + new_word\n",
    "\n",
    "\t\twords = list(map(lambda x: convert(x) if check(x) else x, words))\n",
    "\n",
    "\t\treturn \" \".join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        sentence = sentence.split()\n",
    "        discount = 1.0 - 1.0 * discount / 100 \n",
    "         \n",
    "        res = []\n",
    "\n",
    "        for word in sentence:\n",
    "            if word.startswith('$') and word[1:].isdigit():\n",
    "                word = f'${int(word[1: ]) * discount :.2f}'\n",
    "\n",
    "            res.append(word)\n",
    "\n",
    "        return ' '.join(res)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def discountPrices(self, sentence: str, discount: int) -> str:\n",
    "        lst = sentence.split(' ')\n",
    "        ans = [s if not (s.startswith('$') and s[1:].isdigit()) else '${:.2f}'.format(int(s[1:]) * (100 - discount) / 100) for s in lst]\n",
    "        ans = ' '.join(ans)\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
